package com.yingsheng.bi.hks.repository.redis;

import com.yingsheng.bi.hks.basic.PUser;
import com.yingsheng.bi.hks.basic.utils.HttpUtils;
import com.yingsheng.bi.hks.basic.utils.Utils;
import com.yingsheng.bi.hks.config.props.ChannelSmsProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

@ConfigurationProperties(prefix = "cache.sms")
@Component
public class SubjectRedisService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private ChannelSmsProperties channelSmsProperties;

    private String userKey(String identity) {
        String userKey = "USER#";
        return userKey + identity;
    }

    public PUser getUser(String identity) {
        return (PUser) redisService.get(userKey(identity));
    }

    public void setUser(String identity, PUser user) {
        redisService.set(userKey(identity), user);
    }

    public void delUser(String identity) {
        redisService.del(userKey(identity));
    }

    private String permissionKey(String identity) {
        String permissionKey = "PERMISSION#";
        return permissionKey + identity;
    }

    public void setRolePermission(String roleId, List<String> permissions) {
        redisService.set(permissionKey(roleId), permissions);
    }

    public List<String> getRolePermission(String roleId) {
        return (List<String>) redisService.get(permissionKey(roleId));
    }

    private String smsCodeKey(String identity) {
        String smsCodeKey = "SMS#";
        return smsCodeKey + identity;
    }

    public String genSmsCode(String channel, String mobile, String scene) {
        String identity = channel + "@" + mobile + "@" + scene;
        String code = Utils.genSmsCode();
        long timeout = getExpireTime(channel, scene);
        redisService.set(smsCodeKey(identity), code, timeout);
        return code;
    }

    public boolean validSmsCode(String channel, String mobile, String code) {
        String scene = HttpUtils.getRequestCode();
        return validSmsCode(channel, mobile, scene, code);
    }

    public boolean validSmsCode(String channel, String mobile, String scene, String code) {
        String cacheCode = getSmsCode(channel, mobile, scene);
        if (StringUtils.hasText(cacheCode)) {
            return cacheCode.equals(code);
        }
        return false;
    }

    public String getSmsCode(String channel, String mobile, String scene) {
        String identity = channel + "@" + mobile + "@" + scene;
        return (String) redisService.get(smsCodeKey(identity));
    }

    public long getExpireTime(String channel, String key) {
        return channelSmsProperties.getExpireTime(channel, key);
    }

    /**
     * 短信发送频率校验
     *
     * @param channel 渠道标识
     * @param mobile  手机号
     * @param scene   场景码
     * @return 发送频率校验结果, true-频繁请求, false-未频繁请求
     */
    public boolean smsSendLimitCheck(String channel, String mobile, String scene) {
        if (channelSmsProperties.sendLimitCheck(channel)) {
            return StringUtils.hasText(getSmsCode(channel, mobile, scene));
        }
        return false;
    }

}
